package com.example.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;
import com.example.entry.Notice;
import com.example.entry.OrderGrabbing;
import com.example.service.IOrderGrabbingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/*
 * 骑手抢单后台管理控制器
 * */
@RestController
@RequestMapping("/admin/order-grabbing")
public class OrderGrabbingController {

    @Autowired
    private IOrderGrabbingService orderGrabbingService;

    /*
     * 根据id查询信息
     * */
    @GetMapping("/id={id}")
    public Result getOrderGrabbingOne(@PathVariable Integer id){
        OrderGrabbing byId = orderGrabbingService.getById(id);
        return Result.success(byId);
    }

    /*
     * 根据id进行删除
     * */
    @GetMapping("/delete/id={id}")
    public Result deleteOrderGrabbingInfo(@PathVariable Integer id) {
        orderGrabbingService.removeById(id);
        return Result.success("删除成功");
    }

    /*
     * 批量删除
     * */
    @PostMapping("/batchd_delete")
    public Result batchDelete(@RequestBody List<Integer> ids){
        orderGrabbingService.removeBatchByIds(ids);
        return Result.success("删除成功");
    }

    /*
     * 修改或保存信息
     * */
    @PostMapping("/update")
    public Result updateOrSavOrderGrabbing(@RequestBody OrderGrabbing orderGrabbing){
        orderGrabbingService.saveOrUpdate(orderGrabbing);
        return Result.success("更新成功");
    }

    /*
     * 分页查询
     * */
    @GetMapping("/page")
    public Result getPageOrderGrabbing(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                                @RequestParam(defaultValue = "") Integer status,
                                @RequestParam(defaultValue = "") String nickName,
                                @RequestParam(defaultValue = "") String productName,
                                @RequestParam(defaultValue = "") String riderName){
        QueryWrapper<OrderGrabbing> queryWrapper = new QueryWrapper<>();
        // 根据id进行降序排序
        queryWrapper.orderByDesc("id");
        // 查询管理员和普通用户
        if(Objects.nonNull(status)){
            queryWrapper.like("status", status);
        }
        // 根据条件进行模糊查询
        if(!"".equals(nickName)){
            queryWrapper.like("nick_name", nickName);
        }
        if(!"".equals(productName)){
            queryWrapper.like("product_name", productName);
        }
        if(!"".equals(riderName)){
            queryWrapper.like("rider_name", riderName);
        }
        // 分页构造器
        Page<OrderGrabbing> page = orderGrabbingService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }
}
